<?php

class BUIWXMenu extends BUIMenu {

    public $menu = array();
    public $owner = null;

    public function getMenuUrl($route = 'site/index', $data = null) {
        if($route == NULL)
            $route = 'site/index';
        
        $name = $this->getFullName('|');
        //$spaceId = 'wxsp_' . Blyn::app()->getActiveSpace()->getId();
        if ($this->owner instanceof BOperation)
            $nodeId = $this->owner->getId();
        else
            $nodeId = get_class($this->owner) . '_' . $this->owner->getId();
        $request = yii::app()->request;
        $hostInfo = $request->hostInfo;
        //$wxControl = new BUIWXControl();
        //$url = $wxControl->getLinkUrl($name, array('wxop' => $nodeId, 'menu' => md5($name)), $this->owner, $route);

        $urlData = array(
            'dv' => 'wx',
            'sp' => Blyn::app()->getActiveSpace()->getId(),
            'op' => $nodeId,
            //'menu' => md5($name))
            'mu' => $this->getId());
        if (is_array($data)) {
            $urlData += $data;
        }
        $url = yii::app()->getController()->createUrl($route, $urlData);

        return $hostInfo . $url;
    }

    public function isMenuClicked($menuName, $owner = NULL, $data = NULL) {

        $params = $_REQUEST;
        $rqSpaceId = NULL;
        $rqNodeId = NULL;
        $rqMenuId = NULL;
        if (isset($params['sp'])) {
            $rqSpaceId = $params['sp'];
        }
        if (isset($params['op'])) {
            $rqNodeId = $params['op'];
        }
        if (isset($params['mu'])) {
            $rqMenuId = $params['mu'];
        }
        if ($rqSpaceId == NULL || $rqNodeId == NULL || $rqMenuId == NULL)
            return FALSE;

        $mySpaceId = Blyn::app()->getActiveSpace()->getId();
        //$myNodeId = 'myNodeId';        
        if (!$owner instanceof BEntity)
            $owner = $this->owner;
        if ($owner instanceof BOperation)
            $myNodeId = $owner->getId();
        else
            $myNodeId = get_class($owner) . '_' . $owner->getId();
        $myMenu = $this->getRoot()->getChild($menuName);
        if ($myMenu != NULL) {
            $myMenuId = $myMenu->getId();
            if ($mySpaceId == $rqSpaceId && $myNodeId == $rqNodeId && $myMenuId == $rqMenuId)
                return TRUE;
        }

        return FALSE;
    }

    public function getOperationList($data = NULL) {
        $apps = Blyn::app()->getActiveSpace()->getApps();

        $opList = array();

        foreach ($apps as $app) {
            if ($app instanceof BApp) {
                $ops = $app->getOperations();
                foreach ($ops as $op) {
                    $name = $app->getName() . ":";
                    if ($op instanceof BOperation) {
                        $name .= $op->getName();
                        $opList[$op->getId()] = $name;
                    }
                }
            }
        }

        return $opList;
    }

}

?>
